--- title: "01-Java 备忘单 - Java 教程 - 编程导航教程" created: 2025-12-02 tags: - Java --- # Java 备忘单 - Java 教程 - 编程导航教程 > [!note] 本篇定位 > 专题深挖与资料:备忘单、反射深入、数组汇总、XML 解析、书籍推荐等 10 篇。本篇:**Java 备忘单 - Java 教程 - 编程导航教程**。正文为教程原貌(原话只增不改),代码块语言标记已补全。 这份备忘单是 Java 初学者的速成课程,有助于复习 Java 语言的基本语法。 ## 开始 ### hello.java ```java public class Hello { // main methord public static void main(String[] args) { // Output: Hello, world! System.out.println("Hello, world!"); } } ``` 编译和运行 ``` $ javac Hello.java $ java Hello Hello, world! ``` ### 变量 ```java int num = 5; float floatNum = 5.99f; char letter = 'D'; boolean bool = true; String site = "quickref.me"; ``` ### 原始数据类型 | 数据类型 | 尺寸 | 默认 | 范围 | | --- | --- | --- | --- | | `byte` | 1 字节 | 0 | -128 ~ 127 | | `short` | 2 字节 | 0 | -2 15 ~ 2 15 -1 | | `int` | 4 字节 | 0 | -2 31 ~ 2 31 -1 | | `long` | 8 字节 | 0 | -2 63 ~ 2 63 -1 | | `float` | 4 字节 | 0.0f | N/A | | `double` | 8 字节 | 0.0d | N/A | | `char` | 2 字节 | \u0000 | 0 ~ 65535 | | `boolean` | N/A | fasle | true/false | ### 字符串 ```java String first = "John"; String last = "Doe"; String name = first + " " + last; System.out.println(name); ``` 请参阅:字符串 ### 循环 ```java String word = "QuickRef"; for (char c: word.toCharArray()) { System.out.print(c + "-"); } // Outputs: Q-u-i-c-k-R-e-f- ``` 请参阅:循环 ### 数组 ```java char[] chars = new char[10]; chars[0] = 'a' chars[1] = 'b' String[] letters = {"A", "B", "C"}; int[] mylist = {100, 200}; boolean[] answers = {true, false}; ``` 请参阅:数组 ### 交换 ```java int a = 1; int b = 2; System.out.println(a + " " + b); // 1 2 int temp = a; a = b; b = temp; System.out.println(a + " " + b); // 2 1 ``` ### 类型转换 ``` // Widening // byte 10) { System.out.println("I don't"); } else { System.out.println("I also don't"); } ``` 请参阅:条件 ### 用户输入 ```java Scanner in = new Scanner(System.in); String str = in.nextLine(); System.out.println(str); int num = in.nextInt(); System.out.println(num); ``` ## Java 字符串 ### 基本的 ```java String str1 = "value"; String str2 = new String("value"); String str3 = String.valueOf(123); ``` ### 级联 ```java String s = 3 + "str" + 3; // 3str3 String s = 3 + 3 + "str"; // 6str String s = "3" + 3 + "str"; // 33str String s = "3" + "3" + "23"; // 3323 String s = "" + 3 + 3 + "23"; // 3323 String s = 3 + 3 + 23; // 29 ``` ### 字符串生成器 StringBuilder sb = new StringBuilder(10); ``` ┌───┬───┬───┬───┬───┬───┬───┬───┬───┐ | | | | | | | | | | └───┴───┴───┴───┴───┴───┴───┴───┴───┘ 0 1 2 3 4 5 6 7 8 9 ``` sb.append("QuickRef"); ``` ┌───┬───┬───┬───┬───┬───┬───┬───┬───┐ | Q | u | i | c | k | R | e | f | | └───┴───┴───┴───┴───┴───┴───┴───┴───┘ 0 1 2 3 4 5 6 7 8 9 ``` sb.delete(5, 9); ``` ┌───┬───┬───┬───┬───┬───┬───┬───┬───┐ | Q | u | i | c | k | | | | | └───┴───┴───┴───┴───┴───┴───┴───┴───┘ 0 1 2 3 4 5 6 7 8 9 ``` sb.insert(0, "我的"); ``` ┌───┬───┬───┬───┬───┬───┬───┬───┬───┐ | M | y | | Q | u | i | c | k | | └───┴───┴───┴───┴───┴───┴───┴───┴───┘ 0 1 2 3 4 5 6 7 8 9 ``` sb.append("!");1806613115223461889_0.6585288787578987 ``` ┌───┬───┬───┬───┬───┬───┬───┬───┬───┐ | M | y | | Q | u | i | c | k | ! | └───┴───┴───┴───┴───┴───┴───┴───┴───┘ 0 1 2 3 4 5 6 7 8 9 ``` ### 比较 ```java String s1 = new String("QuickRef"); String s2 = new String("QuickRef"); s1 == s2 // false s1.equals(s2) // true "AB".equalsIgnoreCase("ab") // true ``` ### 操作 ```java String str = "Abcd"; str.toUpperCase(); // ABCD str.toLowerCase(); // abcd str.concat("#"); // Abcd# str.replace("b", "-"); // A-cd " abc ".trim(); // abc "ab".toCharArray(); // {'a', 'b'} ``` ### 信息 ```java String str = "abcd"; str.charAt(2); // c str.indexOf("a") // 0 str.indexOf("z") // -1 str.length(); // 4 str.toString(); // abcd str.substring(2); // cd str.substring(2,3); // c str.contains("c"); // true str.endsWith("d"); // true str.startsWith("a"); // true str.isEmpty(); // false ``` ### 不可变 ```java String str = "hello"; str.concat("world"); // Outputs: hello System.out.println(str); ``` --- ```java String str = "hello"; String concat = str.concat("world"); // Outputs: helloworld System.out.println(concat); ``` 一旦创建不能修改,任何修改都会创建一个新的String ## Java 数组 ### 声明 ```java int[] a1; int[] a2 = {1, 2, 3}; int[] a3 = new int[]{1, 2, 3}; int[] a4 = new int[3]; a4[0] = 1; a4[2] = 2; a4[3] = 3; ``` ### 调整 ```java int[] a = {1, 2, 3}; System.out.println(a[0]); // 1 a[0] = 9; System.out.println(a[0]); // 9 System.out.println(a.length); // 3 ``` ### 循环(读取和修改) ```java int[] arr = {1, 2, 3}; for (int i=0; i < arr.length; i++) { arr[i] = arr[i] * 2; System.out.print(arr[i] + " "); } // Outputs: 2 4 6 ``` ### 循环(读取) ```java String[] arr = {"a", "b", "c"}; for (int a: arr) { System.out.print(a + " "); } // Outputs: a b c ``` ### 多维数组 ```java int[][] matrix = { {1, 2, 3}, {4, 5} }; int x = matrix[1][0]; // 4 // [[1, 2, 3], [4, 5]] Arrays.deepToString(matrix) for (int i = 0; i < a.length; ++i) { for(int j = 0; j < a[i].length; ++j) { System.out.println(a[i][j]); } } // Outputs: 1 2 3 4 5 6 7 ``` ### 类型 ```java char[] chars = {'b', 'a', 'c'}; Arrays.sort(chars); // [a, b, c] Arrays.toString(chars); ``` ## Java 条件 ### 运算符 - + - - - \*1806613115223461889_0.21214418365347387 - / - % - =1806613115223461889_0.7807052768238105 - ++ - -- - !1806613115223461889_0.6647155712015371 - --- - == - != - >1806613115223461889_0.9270624521400379 - >= - < - <=1806613115223461889_0.8857197341414993 - --- - && - || - ?:1806613115223461889_0.9704929221984671 - --- 1806613115223461889_0.37576940022544636 - instanceof - --- - ~ - << - >>1806613115223461889_0.30273423588349546 - >>> - & - ^1806613115223461889_0.7868256439121573 - | ### if-else 语句 ```java int k = 15; if (k > 20) { System.out.println(1); } else if (k > 10) { System.out.println(2); } else { System.out.println(3); } ``` ### switch 语句 ```java int month = 3; String str; switch (month) { case 1: str = "January"; break; case 2: str = "February"; break; case 3: str = "March"; break; default: str = "Some other month"; break; } // Outputs: Result March System.out.println("Result " + str); ``` ### 三元运算符 ```java int a = 10; int b = 20; int max = (a > b) ? a : b; // Outputs: 20 System.out.println(max); ``` ## Java 循环 ### For循环 ```java for (int i = 0; i < 10; i++) { System.out.print(i); } // Outputs: 0123456789 ``` --- ```java for (int i = 0,j = 0; i < 3; i++,j--) { System.out.print(j + "|" + i + " "); } // Outputs: 0|0 -1|1 -2|2 ``` ### For-each 循环 ```java int[] numbers = {1,2,3,4,5}; for (int number: numbers) { System.out.print(number); } // Outputs: 12345 ``` 用于循环数组或列表 ### While 循环 ```java int count = 0; while (count < 5) { System.out.print(count); count++; } // Outputs: 01234 ``` ### Do While 循环 ```java int count = 0; do{ System.out.print(count); count++; } while (count < 5); // Outputs: 01234 ``` ### Continue ```java for (int i = 0; i < 5; i++) { if (i == 3) { continue; } System.out.print(i); } // Outputs: 01245 ``` ### Break ```java for (int i = 0; i < 5; i++) { System.out.print(i); if (i == 3) { break; } } // Outputs: 0123 ``` ## Java 集合框架 ### Java 集合 | 集合 | 接口 | 可组织 | 可排序 | 线程安全 | 可复制 | 可为空 | | --- | --- | --- | --- | --- | --- | --- | | [ArrayList](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) | List | Y | N | N | Y | Y | | [Vector](https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html) | List | Y | N | Y | Y | Y | | [LinkedList](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html) | List, Deque | Y | N | N | Y | Y | | [HashSet](https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html) | Set | N | N | N | N | One `null` | | [LinkedHashSet](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashSet.html) | Set | Y | N | N | N | One `null` | | [TreeSet](https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html) | Set | Y | Y | N | N | N | | [HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html) | Map | N | N | N | N(key) | One `null` (key). | | [HashTable](https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html) | Map | N | N | Y | N(key) | N (key) | | [LinkedHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) | Map | Y | N | N | N(key) | One `null` (key) | | [TreeMap](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html) | Map | Y | Y | N | N(key) | N (key) | | [ArrayDeque](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html) | Deque | Y | N | N | Y | N | | [PriorityQueue](https://docs.oracle.com/javase/8/docs/api/java/util/PriorityQueue.html) | Queue | Y | N | N | Y | N | ### 数组列表 ```java List nums = new ArrayList<>(); // Adding nums.add(2); nums.add(5); nums.add(8); // Retrieving System.out.println(nums.get(0)); // Indexed for loop iteration for (int i = 0; i < nums.size(); i++) { System.out.println(nums.get(i)); } nums.remove(nums.size() - 1); nums.remove(0); // VERY slow for (Integer value : nums) { System.out.println(value); } ``` ### 哈希表 ```java Map m = new HashMap<>(); m.put(5, "Five"); m.put(8, "Eight"); m.put(6, "Six"); m.put(4, "Four"); m.put(2, "Two"); // Retrieving System.out.println(m.get(6)); // Lambda forEach m.forEach((key, value) -> { String msg = key + ": " + value; System.out.println(msg); }); ``` ### 哈希集 ```java Set set = new HashSet<>(); if (set.isEmpty()) { System.out.println("Empty!"); } set.add("dog"); set.add("cat"); set.add("mouse"); set.add("snake"); set.add("bear"); if (set.contains("cat")) { System.out.println("Contains cat"); } set.remove("cat"); for (String element : set) { System.out.println(element); } ``` ### 数组Deque ```java Deque a = new ArrayDeque<>(); // Using add() a.add("Dog"); // Using addFirst() a.addFirst("Cat"); // Using addLast() a.addLast("Horse"); // [Cat, Dog, Horse] System.out.println(a); // Access element System.out.println(a.peek()); // Remove element System.out.println(a.pop()); ``` ## 杂项 ### 访问修饰符 | 修饰符 | 类 | 包 | 子类 | 全局 | | --- | --- | --- | --- | --- | | public | Y | Y | Y | Y | | protected | Y | Y | Y | N | | no modifier | Y | Y | N | N | | private | Y | N | N | N | ### 常用表达 ```java String text = "I am learning Java"; // Removing All Whitespace text.replaceAll("\\s+", ""); // Splitting a String text.split("\\|"); text.split(Pattern.quote("|")); ``` 请参阅:Java 中的正则表达式 ### 文本注释 ``` // I am a single line comment! /* And I am a multi-line comment! */ /** * This * is * documentation * comment */ ``` ### 关键词 - abstract - continue - for - new - switch - assert - default - goto - package - synchronized - boolean - do - if - private - this - break - double - implements - protected - throw - byte - else - import - public - throws - case - enum - instanceof - return - transient - catch - extends - int - short - try - char - final - interface - static - void - class - finally - long - strictfp - volatile - const - float - native - super - while ### 数学方法 | 方法 | 描述 | | --- | --- | | `Math.max(a,b)` | a 和 b 的最大值 | | `Math.min(a,b)` | a 和 b 的最小值 | | `Math.abs(a)` | 绝对值a | | `Math.sqrt(a)` | a 的平方根 | | `Math.pow(a,b)` | a的b次方 | | `Math.round(a)` | 最接近的整数 | | `Math.sin(ang)` | 正弦 | | `Math.cos(ang)` | ang的余弦 | | `Math.tan(ang)` | ang 的切线 | | `Math.asin(ang)` | ang 的反正弦 | | `Math.log(a)` | a 的自然对数 | | `Math.toDegrees(rad)` | 角度 rad | | `Math.toRadians(deg)` | 以弧度为单位的角度度 | ### try/catch/finally ```java try { // something } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("always printed"); } ``` --- ⬅️ [[13-Java 实例|Java 实例]] 🏠 [[00-鱼皮学习路线|鱼皮学习路线]] ➡️ [[02-java switch基础介绍及具体使用 - Java 教程 - 编程导航教程|java switch基础介绍及具体使用 - Java 教程 - 编程导航教程]]